import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from "element-ui";
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login' // 添加重定向
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/office/index',
    name: 'Index',
    component: () => import('@/views/Index.vue'),
  },
  {
    path: '/office',
    name: 'Container',
    component: () => import('@/components/container/Container.vue'),

    children: [
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue')
      },
      {
        path: 'meeting',
        name: 'Meeting',
        component: () => import('@/views/meeting/Meeting.vue')
      },
      {
        path: 'dates',
        name: 'Dates',
        component: () => import('@/views/dates/Dates.vue')
      },
      {
        name: 'Task',
        path: 'task',
        component: () => import('@/views/task/Task.vue')
      },
      {
        path: 'first',
        name: 'first',
        component: () => import('@/views/forum/first.vue')
      },
      {
        path: 'first/detail/:id',
        name: 'detail',
        component: () => import('@/views/forum/detail.vue')
      },
      {
        path: 'first/PostForm',
        name: 'PostForm',
        component: () => import('@/views/forum/PostForm.vue')
      },
      { path: '/first/UserProfile/:userId/:tab',
        name: 'UserProfile',
        component: () => import('@/views/forum/UserProfile.vue'),
        children: [
          {
            path: 'posts',
            name: 'UserPosts',
            component: () => import('@/views/forum/postList.vue')
          },
          {
            path: 'comments',
            name: 'UserComments',
            component: () => import('@/views/forum/CommentList.vue')
          },
          {
            path: 'following',
            name: 'FollowUser',
            component: () => import('@/views/forum/FollowUser.vue'),
          },
          {
            path: 'likes',
            name: 'UserLikes',
            component: () => import('@/views/forum/LikesList.vue')
          },
        ]
      },
      {
        path: 'following/:userId',
        name: 'followProfile',
        component: () => import('@/views/forum/followProfile.vue')
      },
      {
        path: 'collect',
        name: 'collect',
        component: () => import('@/views/document/Related/collect.vue')
      },
      {
        path: 'KnowledgeBase',
        name: 'KnowledgeBase',
        component: () => import('@/views/document/KnowledgeBase/KnowledgeBase.vue')
      },
      {
        path: 'share',
        name: 'share',
        component: () => import('@/views/document/share/share.vue')
      },
      {
        path: 'RecycleBin',
        name: 'RecycleBin',
        component: () => import('@/views/document/RecycleBin/RecycleBin.vue')
      },
      {
        path: 'search',
        name: 'search',
        component: () => import('@/views/document/search/search.vue')
      },
      {
        path: 'ChatWindow',
        name: 'ChatWindow',
        component: () => import('@/components/ChatWindow.vue')
      },
    ]
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  if (to.path === '/login' || to.path === '/register' || to.path === '/') {
    next()
  } else {
    const token = localStorage.getItem('token');
    if (token == null) {
      Message.error('请先登录')
      router.push({ name: 'Login' }).catch(() => {
      })
    } else {
      // 获取一下用户信息
      store.dispatch('user/GetInfo').then()
      next()
    }
  }
})

export default router
